package com.health.trans.processor.reader;

import com.health.trans.annotation.Reader;
import com.health.trans.kit.ReaderConfig;
import com.health.trans.pojo.SqlSentence;
import com.health.trans.util.DataSourceUtil;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.database.JpaPagingItemReader;
import org.springframework.batch.item.database.orm.JpaNativeQueryProvider;

/**
 * @author nqlz
 */
@Reader
public class JpaReader implements CsReader {

    public  <T> ItemReader<T> getReader(Class<?> tClass, String dataSource, String otherCondition, int size, int maxCount) throws UnexpectedInputException, ParseException, NonTransientResourceException {
        //读取数据,这里可以用JPA,JDBC,JMS 等方式 读入数据
        JpaPagingItemReader<T> reader = new JpaPagingItemReader<>();
        String sqlQuery = SqlSentence.getSelectSql(tClass, otherCondition);
        try {
            JpaNativeQueryProvider<T> queryProvider = new JpaNativeQueryProvider<>();
            queryProvider.setSqlQuery(sqlQuery);
            queryProvider.setEntityClass((Class<T>) tClass);
            queryProvider.afterPropertiesSet();
            reader.setEntityManagerFactory(DataSourceUtil.getEntityManagerFactory(dataSource));
            reader.setPageSize(size);
            if (maxCount > 0) {
                reader.setMaxItemCount(maxCount);
            }
            reader.setQueryProvider(queryProvider);
            reader.afterPropertiesSet();

            //所有ItemReader和ItemWriter实现都会在ExecutionContext提交之前将其当前状态存储在其中,如果不希望这样做,可以设置setSaveState(false)
            reader.setSaveState(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reader;
    }


    @Override
    public <T> ItemReader<T> getReader(ReaderConfig<T> readerConfig) {
        JpaPagingItemReader<T> reader = new JpaPagingItemReader<>();
        try {
            JpaNativeQueryProvider<T> queryProvider = new JpaNativeQueryProvider<>();
            queryProvider.setSqlQuery(readerConfig.getSql());
            queryProvider.setEntityClass(readerConfig.getTableClass());
            queryProvider.afterPropertiesSet();
            reader.setEntityManagerFactory(DataSourceUtil.getEntityManagerFactory(readerConfig.getSourceName()));
            reader.setPageSize(readerConfig.getPageSize());
            int maxCount = readerConfig.getMaxCount();
            if (maxCount > 0) {
                reader.setMaxItemCount(maxCount);
            }
            reader.setQueryProvider(queryProvider);
            reader.afterPropertiesSet();

            //所有ItemReader和ItemWriter实现都会在ExecutionContext提交之前将其当前状态存储在其中,如果不希望这样做,可以设置setSaveState(false)
            reader.setSaveState(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reader;
    }

}
